import { ConfTable } from '../lib/ConfTable';

export class API {
  //获取配置表
  public static getConfTable(name: string): ConfTable {
    return ConfTable.getConfTable(name);
  }

  //获取常量
  public static getConst(key: string): any {
    var conf = API.getConfTable('const');
    var data = conf.getRowByIndex('key', key);

    return ConfTable.convert(data.type, data.value);
  }

  //获取文本
  public static getWords(key: string): string {
    var conf = API.getConfTable('chinese');
    var data = conf.getRowByIndex('key', key);
    let words: string = '';
    if (data) words = data.describe;
    return data.describe;
  }

  //获取报错文本
  public static getErrorWords(key: number): string {
    var conf = API.getConfTable('chinese_error');
    var data = conf.getRowByIndex('key', key);
    let words: string = '';
    if (data) words = data.describe;
    if (!data) words = '未知错误';
    return words;
  }

  //脚本
  public static getScript(url: string): any {
    // if (API.runtime != "server") {
    //     var list = url.split("/");
    //     url = <string>list.pop();
    // } else {
    //     if (url == null || url == "") return null;
    //     url = "../obj/" + url;
    // }
    var list = url.split('/');
    url = <string>list.pop();
    if (url == null || url == '') return null;

    return require(url);
  }
}
